import pymysql


class DB(object):
    def __init__(self ,database ,user ,password ,charset = 'utf8' ,host = 'localhost' ,port = 3306):
        self.host = host
        self.port = port
        self.database =database
        self.user = user
        self.password =password
        self.charset = charset

class MakeMysql(object):
    def __init__(self ,dB):
        self.db = pymysql.connect(
            host = dB.host,
            port = dB.port,
            database = dB.database,
            user = dB.user,
            password = dB.password,
            charset = dB.charset
        )
        self.cursor = self.db.cursor()


    def updateDeleteInsert(self ,sql ,values = None):
        if values != None:
            sql = sql % values
        try:
            self.cursor.execute(sql)
            self.db.commit()
        except Exception as e:
            print("操作异常")

    def select(self ,sql ,num):
        try:
            self.cursor.execute(sql)
            if num == 'all':
                return self.cursor.fetchall()
            elif num == 1:
                return self.cursor.fetchone()
            else:
                return self.cursor.fetchmany(num)
        except Exception as e:
            print("操作异常")

    def __del__(self):
        self.cursor.close()
        self.db.close()